package com.cnpinxi.service;

import com.cnpinxi.dao.ResourceRelationMapper;
import com.cnpinxi.domain.ProductEntity;
import com.cnpinxi.domain.ResourceRelation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by Baggio on 2017-11-15.
 */
@Service
public class ResourceRelationServiceImpl implements com.cnpinxi.service.ResourceRelationService {
    @Autowired
    ResourceRelationMapper resourceRelationMapper;
    @Autowired ProductEntityService productEntityService;

    @Override
    public List<ResourceRelation> getResourceRelationList(Map<String, String> map) {
        return resourceRelationMapper.getResourceRelationList(map);
    }

    @Override
    public ResourceRelation getResourceRelation(Map<String, String> map) {
        return resourceRelationMapper.getResourceRelationt(map);
    }

    @Override
    public void modifyResourceRelation(ResourceRelation resourceRelation) {
        Long resourceRelationId = resourceRelation.getTid();
        if(resourceRelation.getResourceRelationType()==ResourceRelation.RESOURCE_RELATION_TYPE_PRODUCT){
            ProductEntity productEntity=productEntityService.getProductEntityById(resourceRelation.getResourceRelationId());
            resourceRelation.setResourceRelationId(productEntity.getProductId());
        }
        if (resourceRelationId == null) {
            insertResourceRelation(resourceRelation);
        } else {
            updateResourceRelation(resourceRelation);
        }
    }

    private void insertResourceRelation(ResourceRelation resourceRelation) {
        resourceRelationMapper.insertResourceRelation(resourceRelation);
    }

    private void updateResourceRelation(ResourceRelation resourceRelation) {
        resourceRelationMapper.updateResourceRelationByPrimaryKeySelective(resourceRelation);
    }

    @Override
    public void deleteResourceRelation(Long tid) {
        resourceRelationMapper.deleteById(tid);
    }
}

